package com.zlm.zy.zhoujuanjuan.a;

import android.util.Base64;

/**
 * 创建时间: 2018/11/30 16:35
 * 作者: zhoujuanjuan
 * 描述: 编码加密
 */
public class EncodeUtils {

  private static final String CHARACTER = "UTF-8";

  private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
      '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

  /**
   * base64解密
   */
  public static byte[] base64Decode(String key) {
    return Base64.decode(key, Base64.NO_WRAP);
  }

  /**
   * base64加密
   */
  public static String base64Encode(byte[] data) {
    String str = new String(Base64.encode(data, Base64.NO_WRAP));
    return str;
  }

  /**
   * byte[] to 十六进制
   *
   * @param bytes
   * @return
   */
  public static String bytesToHexString(byte[] bytes) {
    char[] buf = new char[bytes.length * 2];
    int index = 0;
    for(byte b : bytes) { // 利用位运算进行转换，可以看作方法一的变种
      buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
      buf[index++] = HEX_CHAR[b & 0xf];
    }

    return new String(buf);
  }
}
